//
//  MVClassifyCollectionViewCell.m
//  NewMusic
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "MVClassifyCollectionViewCell.h"
#import "MVClassifyDetailCollectionViewCell.h"
#import "TheNewMVModel.h"
#import "MVTypeModel.h"
#import "TheNewMVViewController.h"
#import <MJRefresh.h>
@interface MVClassifyCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic, copy) NSString *order;
@property(nonatomic, retain) MVTypeModel *model;
@property(nonatomic, assign) NSInteger page;
@property(nonatomic, assign) NSInteger totalPage;
@end

@implementation MVClassifyCollectionViewCell
- (void)dealloc {
    [_titleString release];
    [_order release];
    [_model release];
    [_collectionView release];
    [_dataArray release];
    [_header release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = [NSMutableArray array];
        self.model = [MVTypeModel shareMVTypeModel];
        self.page = 1;
        //创建视图
        [self createSubView];
        if (_model.ID == nil) {
            [self loadData:@"2" idString:@"0"];
        }else {
            [self loadData:@"2" idString:_model.ID];
        }
    }
    [self loadNewData];
    return self;
}
- (void)loadNewData {
self.collectionView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
    self.page++;
    if (self.page > self.totalPage) {
        [self.collectionView.footer noticeNoMoreData];
    }else {
        if (_model.ID == nil) {
            [self loadData:@"2" idString:@"0"];
        }else {
            [self loadData:@"2" idString:_model.ID];
        }
    }
}];
}
#pragma mark 获取数据
- (void)loadData:(NSString *)orderString idString:(NSString *)idString{
    NSLog(@"http://api.dongting.com/song/video/type/%@?order=%@&page=%ld&size=12&app=ttpod&v=v8.1.1.2015110616", idString,orderString, self.page);
     [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat:@"http://api.dongting.com/song/video/type/%@?order=%@&page=%ld&size=12&app=ttpod&v=v8.1.1.2015110616", idString,orderString, self.page] andParameters:nil andBlock:^(id response) {
         self.totalPage = [[response valueForKey:@"pageCount"] integerValue];
      for (NSDictionary *dic in [response valueForKey:@"data"]) {
          TheNewMVModel *model = [[TheNewMVModel alloc]initWithDictionary:dic];
          [_dataArray addObject:model];
          [model release];
      }
         [self.collectionView.footer endRefreshing];
      [_collectionView reloadData];
  }];
}
#pragma mark 创建视图
- (void)createSubView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    //注册cell的重用标识
    [_collectionView registerClass:[MVClassifyDetailCollectionViewCell class] forCellWithReuseIdentifier:@"MVClassifyDetailCollectionViewCellIdentify"];
    //注册头视图
    [_collectionView registerClass:[MVHotCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.contentView addSubview:_collectionView];
    [flowLayout release];
    [_collectionView release];
}
#pragma mark cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MVClassifyDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MVClassifyDetailCollectionViewCellIdentify" forIndexPath:indexPath];
    if (_dataArray.count != 0) {
        cell.newMVModel = _dataArray[indexPath.row];
    }
    return cell;
}
#pragma mark 头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    self.header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    if (self.model.name == nil) {
         _header.titleString = @"全部";
    }else {
    _header.titleString = self.model.name;
    }
    //全部
    [_header.allButton addTarget:self action:@selector(allButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _header.HotButton.tag = 2300;
    //最热
    [_header.HotButton addTarget:self action:@selector(hotButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _header.NewButton.tag = 2301;
    //最新
    [_header.NewButton addTarget:self action:@selector(newButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    return _header;
}
#pragma mark cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count ? _dataArray.count : 0;
}
#pragma mark cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((self.width - 30) / 2, self.height / 4);
}
#pragma mark 头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(self.width, 40);
}
#pragma mark cell的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}
#pragma mark 最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}
#pragma mark cell的四周间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5, 10, 5, 10);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate everyItemClickedAction:[[_dataArray objectAtIndex:indexPath.row] mvList]];
}
#pragma mark 全部的button的点击事件
- (void)allButtonAction:(UIButton *)button {
    [self.delegate allButtonClickedAction:button.titleLabel.text];
}
#pragma mark 最热button的点击事件
- (void)hotButtonAction:(UIButton *)button {
    [button setTitleColor:[UIColor colorWithRed:56 / 255.0 green:192 / 255.0 blue:107 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    UIButton *btn = (UIButton *)[self viewWithTag:2301];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.dataArray = [NSMutableArray array];
    if (_model.ID == nil) {
        [self loadData:@"2" idString:@"0"];
    }else {
        [self loadData:@"2" idString:_model.ID];
    }
    [_collectionView reloadData];
}
#pragma mark 最新button的点击事件
- (void)newButtonAction:(UIButton *)button {
    [button setTitleColor:[UIColor colorWithRed:56 / 255.0 green:192 / 255.0 blue:107 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    UIButton *btn = (UIButton *)[self viewWithTag:2300];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.dataArray = [NSMutableArray array];
    if (_model.ID == nil) {
        [self loadData:@"1" idString:@"0"];
    }else {
        [self loadData:@"1" idString:_model.ID];
    }
    [_collectionView reloadData];
}
@end
